#ifndef SQLOPERATOR_H
#define SQLOPERATOR_H
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlRecord>
#include <QSqlField>

#include <QDebug>
#include <QMessageBox>
#include <QObject>

#include "Geo/map/geomap.h"
#include "Geo/map/geolayer.h"
#include "Geo/map/geofielddefn.h"
#include "Geo/map/geofeature.h"
#include "Geo/utility/filereader.h"
#include "util/utility.h"
#include <gdal/gdal_priv.h>
#include <gdal/gdal_frmts.h>
#include <ogrsf_frmts.h>
#include <ogr_geometry.h>
#include <ogr_feature.h>
#include "util/env.h"

//SQLOperator Singleton pattern
class SQLOperator : public QObject
{
    Q_OBJECT
public:
    static SQLOperator* getInstance(QString dbName);

public:
    bool createConnect(QString host,int port,QString user,QString password,QString database);
    bool createDatabase(QString dbName);
    bool createBasicsTable();
    bool dropFeatureLayer(QString layerName);
    void newFeatureLayer(QString layerName,QString geomtype,int srid);
    bool changeDatabase(QString dbName);
    void importFeatureLayer(GeoFeatureLayer *layer);
    void addFeature(GeoFeatureLayer *layer,GeoFeature *feature);
    void deleteFeature(GeoFeatureLayer *layer,GeoFeature *feature);
    bool deleteDatabase(QString dbName);
    GeoFeatureLayer* importShpfile(QString filepath);
    GeoFeatureLayer* readFeatureLayer(QString layerName,GeometryType type);
    void quit();

    void addField(QString layerName,QString fieldName,GeoFieldType fieldType);
    void deleteField(GeoFeatureLayer *layer,QString fieldName);
    QVector<QString> showDatabases();

    QMap<QString,int> getAllLayer();
    QVector<QString> getAllScheme();

private:
    explicit SQLOperator(QString dbName,QObject *parent = nullptr);
    ~SQLOperator();
    static QSqlDatabase gdb;
    static SQLOperator* sqlInstance;

    static QString mysqlHost;
    static int mysqlPort;
    static QString mysqlUser;
    static QString mysqlPassword;
    //static QString mysqlDatabase;
};
#endif // SQLOPERATOR_H
